layui.use(['layer', 'form', 'sysweb', 'dtree'], function () {
    var $ = layui.$
        , layer = layui.layer
        , form = layui.form
        , sysweb = layui.sysweb
        , dtree = layui.dtree

    var DemoTree;
    var treeLevel = '1';// 记录数展开的等级

    /**
     * 选择一个数的等级
     */
    $(".treeLevel").click(function () {
        selectTreeLevel($(this).html());
    });

    function selectTreeLevel(val) {
        var className = "layui-btn-primary";
        $(".treeLevel").addClass(className);
        $(".treeLevel").each(function () {
            var othis = $(this);
            if (val == othis.html()) {
                othis.removeClass(className);
            }
        });
        treeLevel = val;
        dtree.reload(DemoTree, {
            initLevel: treeLevel
        });
    }

    function queryTreeData(fun) {
        sysweb.req({
            url: basePath + '/node/tree'
            , done: function (data) {
                var list = data.result;
                fun(list);
            }
        });
    }

    /**
     * 初次加载树结构
     */
    queryTreeData(function (list) {
        $("#treeView").html('<ul id="demoTree" class="dtree"></ul>');
        DemoTree = dtree.render({
            elem: "#demoTree"
            , initLevel: treeLevel
            , menubar: true
            , data: list
            , toolbar: true     //开启右键工具栏
            , toolbarScroll: "#treeView" // 父级菜单id
            , toolbarShow: []
            , toolbarExt: [
                {
                    toolbarId: "addNode"
                    , icon: "dtree-icon-roundadd"
                    , title: "下载", handler:
                        function (node) {downloadFile(node);}
                }
                , {
                    toolbarId: "delNode"
                    , icon: "dtree-icon-roundclose"
                    , title: "删除", handler:
                        function (node) {deleteFile(node);}
                }
            ]
        });
    });

    /**
     * 下载文件
     * @param node
     */
    function downloadFile(node) {
        var n = JSON.parse(node.basicData);
        if (node.isLeaf) {
            window.location = n.src;
        } else {
            layer.msg("文件夹不能下载");
        }
    }

    /**
     * 删除文件
     * @param node
     */
    function deleteFile(node) {
        var n = JSON.parse(node.basicData);
        layer.confirm('确定要删除文件 ' + node.context + '么?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            layer.close(index);
            sysweb.load();
            sysweb.req({
                url: basePath + '/delete'
                , async: false
                , data: {path: n.src}
                , done: function (data) {
                    layer.alert("删除成功", {
                        icon: 1
                    }, function (index1) {
                        layer.close(index1);
                        queryTreeData(function (treeList) {
                            dtree.reload(DemoTree, {
                                data: treeList
                            });
                        });
                    });
                }
            });
        });
    }

    var img = Array.of(".jpg", ".png", ".gif");
    var txt = Array.of(".txt", ".json", ".xml");

    function endsWith(str, arr) {
        var flg = false;
        for (let char of arr) {
            flg = flg || (str.endsWith(char));
        }
        return flg;
    }

    // 点击节点
    dtree.on("node('demoTree')", function (obj) {
        // 判断是否是叶子节点
        var node = obj.param;
        console.log(obj);
        if (node.isLeaf) {
            // 如果是图片
            var n = JSON.parse(node.basicData);
            if (endsWith(n.path, img)) {
                $("#imgDiv").show();
                $("#txtDiv").hide();

                var src = n.src;
                // src = n.src.replace(new RegExp('//', 'g'), '/');
                // src = n.src.replace(new RegExp('\\', 'g'), '/');

                $("#imgSrc").html(src);

                var html = '<div class="layer-photos-demo">' +
                    '<img style="width: 100px;height: 100px;cursor: pointer;" layer-pid="' + node.id + '" layer-src="' + src + '" src="' + src + '"  />' +
                    '</div>';
                $("#img").html(html);

                layer.photos({
                    photos: '.layer-photos-demo'
                    , anim: 5
                });
            } else if (endsWith(n.path, txt)) {
                $("#txtDiv").show();
                $("#imgDiv").hide();

                queryFile(n.path);
            } else {
                layer.msg("无法显示该格式");
            }
        } else {

        }
    });

    /**
     * 查询文件内容
     * @param path
     */
    function queryFile(path) {
        sysweb.req({
            url: basePath + '/node/getfilecontent'
            , cache: false
            , async: false
            , data: {path: path}
            , done: function (data) {
                var list = data.result;
                $("#code").html('');
                //<p>&lt;&gt;</p>
                for (var i = 0; i < list.length; i++) {
                    // 全局替换
                    var all = list[i].replace(new RegExp("&", 'gm'), "&amp;").replace(new RegExp("<", 'gm'), "&lt;").replace(new RegExp(">", 'gm'), "&gt;");
                    $("#code").append('&nbsp;' + all + "<br/>");
                }
            }
        });
    }

});